#include "common.h"
VOID  Schedule()
{
    //Print("z");
    //return;
    heartbeat++;
 //   WhoRunning=WhoRunning->p_pprocessnext;
   if(WhoRunning==PTR_INVALID) 
   {
      return;
   }

   
    if(WhoRunning->p_state==STATE_RUNNING)
    {   
         WhoRunning->p_state=STATE_READY;
	}

   PPROCESS ppro=process->p_pprocessprev;
   
   do{
      ppro=ppro->p_pprocessnext;
      if(ppro->p_state==STATE_WAIT )
      {
          if(ppro->p_waittick==1)
          {
            ppro->p_waittick=0;
            ppro->p_signalreturn=INVALID;
            ppro->p_state=STATE_READY;
          }
          else 
          {
             if(ppro->p_waittick>0)
             {
                ppro->p_waittick--;
			 }
          }
      }
   }
   while(ppro!=PTR_INVALID && ppro->p_pprocessnext!=process);
  

    PPROCESS pro=WhoRunning;
	
    do{
         WhoRunning=WhoRunning->p_pprocessnext;
         if(pro==WhoRunning)
		 {
		    break;
		 }
      }while(WhoRunning->p_remain==0 || WhoRunning->p_state!=STATE_READY);
	
    if(WhoRunning->p_remain!=0 && WhoRunning->p_state==STATE_READY)
    {
        WhoRunning->p_remain--;
		WhoRunning->p_state=STATE_RUNNING;
		return;
	}
   // if(pro->p_remain!=0 && pro->p_state==STATE_READY){WhoRunning=pro;WhoRunning->p_remain--;WhoRunning->p_state=STATE_RUNNING;return;}
    WhoRunning=pro;
   
    do{
        if(WhoRunning->p_state==STATE_READY)
        {  
            WhoRunning->p_remain=WhoRunning->p_priority;
		}
         WhoRunning=WhoRunning->p_pprocessnext;
      }while(WhoRunning!=pro);
	
    do{
         WhoRunning=WhoRunning->p_pprocessnext;
         if(pro==WhoRunning) 
		 {
		    break;
		 }
      }while(WhoRunning->p_remain==0 || WhoRunning->p_state!=STATE_READY);
	
    if(WhoRunning->p_remain!=0 && WhoRunning->p_state==STATE_READY)
    {
        WhoRunning->p_state=STATE_RUNNING; 
		WhoRunning->p_remain--;
		return;
	}
    
} 
